//
//  ADataBaseHandle.m
//  UI_元旦豆瓣小集合
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "ADataBaseHandle.h"

@implementation ADataBaseHandle

+ (ADataBaseHandle *)sharedDaseBaseHandle {
    static ADataBaseHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[ADataBaseHandle alloc] init];
    });
    return handle;
}

- (void)openDB {
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [document stringByAppendingPathComponent:@"Movie.splite"];
    NSLog(@"沙盒%@", dbPath);
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"open");
    }else {
        NSLog(@"no open");
    }
    
}

- (void)createtable {
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, title)"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"createtable");
    }else {
        NSLog(@"no createable");
    }
}

- (void)insetrModel:(Mytitlemodel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Movie(title) values('%@')", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"insetr");
    }else {
        NSLog(@"no insetr");
    }
}

- (void)deleteModel:(Mytitlemodel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Movie where title = '%@'", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"delete");
    }else {
        NSLog(@"no delete");
    }
}
- (void)deleteModel{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Movie"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"delete");
    }else {
        NSLog(@"no delete");
    }
}

- (NSMutableArray *)selectModel {
    NSString *sqlStr = [NSString stringWithFormat:@"select* from Movie"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arrModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            Mytitlemodel *model = [Mytitlemodel mytitlemodel];
            model.title = [NSString stringWithUTF8String:(const char *) title];
            [arrModel addObject:model.title];
        }
    }else{
        NSLog(@"查询失败");
    }
    return arrModel;
 
    
    
}


    
    

    




@end
